The comma operator takes two expressions, executes them from left to right, and returns the result of the second one. The use of this operator is
generally detrimental to the readability and reliability of code, and the same effect can be achieved by other means.
i = a += 2, a + b; // Noncompliant: What is the value of i?
Writing each expression on its own line will improve readability and might fix misunderstandings.
a += 2;
i = a + b; // We probably expected to assign the result of the addition to i, although the previous code wasn't doing it.
It is especially error-prone in array subscripts - until C++20 - where it might be misinterpreted as accessing a multidimensional array.
a[1, 2] = 3; // Noncompliant: until C++20, 1 is ignored. This is not an access to a multidimensional array.
Using a comma in this context was deprecated in C++20, and a real multi-dimensional subscript operator was introduced in C++23.
Exceptions
The comma operator is tolerated in initializations and increment expressions of for
loops.
for (i = 0, j = 5; i < 6; i++, j++) { ... }